package com.ssm.sfzy.controller;

import com.ssm.sfzy.entity.Book;
import com.ssm.sfzy.entity.TeacherBooks;
import com.ssm.sfzy.entity.TeacherGoods;
import com.ssm.sfzy.service.BookService;
import com.ssm.sfzy.service.TeacherBooksService;
import com.ssm.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName TeacherBooksController
 * @Author xiaoliu
 * @Date 2024年04月07日 15:17
 */
@RestController
@CrossOrigin
@RequestMapping("/teacherBooks")
public class TeacherBooksController {

    @Autowired
    private TeacherBooksService teacherBooksService;
    @Autowired
    private BookService bookService;

    @GetMapping("/findById/{id}")
    public Object findById(@PathVariable Integer id){

        List<TeacherBooks> teacherBooks = teacherBooksService.findAllBooks(id);

        for (TeacherBooks teacherBook : teacherBooks) {
            Book book = bookService.findById(teacherBook.getBookId());
            teacherBook.setBookName(book.getName());
        }

        return Result.success(teacherBooks);
    }

    @PostMapping("/add")
    public Object add(@RequestBody TeacherBooks teacherBooks){
        System.out.println(teacherBooks);
        teacherBooksService.add(teacherBooks);
        return Result.success();
    }

    @DeleteMapping("/remove/{id}")
    public Object remove(@PathVariable Integer id){
        teacherBooksService.remove(id);
        return Result.success();
    }
}
